import * as CryptoJS from '@/config/cryptoJs';
import { Encode } from '@/config/encode';
import { KAY } from '@/config/api';

/** 使用aes加密密码 */
export function aesEncrypt(password, key = KAY) {
    var dk = Encode.base64decode(key);
    var pk = dk.substring(0, 16);
    var iv = dk.substring(16);
    pk = CryptoJS.enc.Utf8.parse(pk);
    iv = CryptoJS.enc.Utf8.parse(iv);
    var encrypted = CryptoJS.AES.encrypt(password, pk,
        {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
    return encrypted.toString();
}

/** 使用aes解密密码 */
export function aesDecrypt(password, key = KAY) {
    var dk = Encode.base64decode(key);
    var pk = dk.substring(0, 16);
    var iv = dk.substring(16);
    pk = CryptoJS.enc.Utf8.parse(pk);
    iv = CryptoJS.enc.Utf8.parse(iv);

    var decrypted = CryptoJS.AES.decrypt(password, pk,
        {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
    return CryptoJS.enc.Utf8.stringify(decrypted).toString();
}
